package com.example.service.impl;


import com.example.controller.Code;
import com.example.dao.BookDao;
import com.example.domain.Book;
import com.example.exception.BusinessException;
import com.example.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;

    public boolean save(Book book) {
        return bookDao.save(book)>0;
    }

    public boolean update(Book book) {
        return bookDao.update(book)>0;
    }

    public boolean delete(Integer id) {
        return bookDao.delete(id)>0;
    }

    public Book getById(Integer id) {
        //将要可能发生的异常进行包装，转换成自定义异常.下面为模拟异常。
        if(id==1){
            throw new BusinessException(Code.BUSINESS_ERR,"请正确输入!");
        }
       /* try{
            int i=1/0;
        }catch (ArithmeticException e){
            throw  new SystemException(Code.SYSTEM_TIMEOUT_ERR,"服务器请求超时，请稍后尝试!");
        }*/
        return bookDao.getById(id);
    }

    public List<Book> getAll() {
        return bookDao.getAll();
    }
}
